﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if DEVELOP
namespace Asset
{
    public class EditorBundle : IAsyncBundle
    {
        protected Dictionary<string, IAsyncAsset> map = null;
        protected string resUrl;
        protected UnityEngine.Object result;
        public UnityEngine.Object editorAsset { get => this.result; set => this.result = value; }
        public EditorBundle(string url)
        {
            this.resUrl = url.ToLower();
        }

        protected virtual void initialize()
        {
            doLoadImm();
        }

        public string URL => this.resUrl;

        protected AsyncStatus asyncStatus = AsyncStatus.Waiting;
        public AsyncStatus Status => this.asyncStatus;

        public AssetBundle bundle => throw new NotImplementedException();

        private int refCount = 0;
        public int RefCount { get => refCount; set => refCount = value; }

        public bool canUnload
        {
            get
            {
                return this.RefCount <= 0 && (this.map == null || this.map.Count <= 0);
            }
        }

        public int loadCostTime => 0;

        public virtual IEnumerator doLoad()
        {
            throw new NotImplementedException();
        }

        public void doLoadImm()
        {
            if (this.Status != AsyncStatus.Finish)
            {
                this.result = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(this.URL);
                this.asyncStatus = AsyncStatus.Finish;
            }
        }

        public virtual T loadAsset<T>(string name) where T : UnityEngine.Object
        {
            this.doLoadImm();
            T t = this.result as T;
            if (t == null)
            {
                t = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(this.URL);
                this.result = t;
            }
            return t;
        }

        public AsyncAsset<T> loadAssetAsync<T>(string name) where T : UnityEngine.Object
        {
            IAsyncAsset asyncAsset;
            if (map == null)
            {
                map = new Dictionary<string, IAsyncAsset>();
            }
            if (map.TryGetValue(name, out asyncAsset))
            {
                EditorAsyncAsset<T> asset = asyncAsset as EditorAsyncAsset<T>;
                return asset;
            }
            else
            {
                EditorAsyncAsset<T> asset = new EditorAsyncAsset<T>(name, this);
                map.Add(name, asset);
                ResPool.Instance.addWaitingAsset(asset);
                return asset;
            }
        }

        public void removeAsyncAsset(string resName)
        {
            if (map != null)
            {
                map.Remove(resName);
            }
        }

        public void unload()
        {
            //if (this.result != null)
            //    Resources.UnloadAsset(this.result);
            Resources.UnloadUnusedAssets();
        }
    }

}
#endif